layui.use(['form','element'], function(){
	form = layui.form(), 		//layui 表单元素修饰
	element = layui.element(); 	//layui 元素监听
	// 加载层
	var load_index = parent.layer.load(2);
	$(function(){
		// 关闭加载层
		parent.layer.close(load_index);
		// 显示内容
		$('body').fadeIn();

		// 自动设置自定义区域layui-form样式的select选项框朝向
		// 数组中添加自定义区域class，class必须设置position属性
		$.each(['manniucms-sub-nav','manniucms-sub-content','manniucms-sub-footer'],function(n,value){
			$('.'+value+' .layui-form-select').each(function(){
				var current_select = $(this);
				// 数组中添加select某个父类的class，此父类需要能获取到相对于自定义区域的位置
				$.each(['layui-form-item','layui-inline'],function(n2,value2){
					if(current_select.closest('.'+value2).length >= 1){
						if(($('.'+value).height() - current_select.closest('.'+value2).position().top) <= current_select.children('.layui-anim').outerHeight(true)){
							current_select.children('.layui-anim').css({
								'top':'-'+(current_select.children('.layui-anim').outerHeight(true)+4)+'px'
							});
						}
					}
				});
			});
		});

		// 刷新页面
		$('.reload').click(function(){
			window.location.href=window.location.href; 
			window.location.reload;
		});
		// 返回上一页
		$('.goback').click(function(){
			window.history.back();
		});

		// checkbox全选
		form.on('checkbox(allChoose)', function(data){
			var child = $(data.elem).parents('form').find('tbody input[type="checkbox"]');
			child.each(function(index, item){
				item.checked = data.elem.checked;
			});
			form.render('checkbox');
		});

		// 监听 网站导航 系统栏目选择，并赋值给URL字段
		form.on('select(select_categorys)', function(data){
			$('#mn_url').val(data.value); //得到被选中的值
		});

		// 监听 商品添加/商品修改页，规格选中项必填
		form.on('checkbox(goods_spec_checkbox)', function(data){
			var a = data.elem.checked;
			if(a == true){
				$(data.elem).parent().children(".markup_input").removeAttr("disabled");
				$(data.elem).parent().children('.markup_input').attr("required","required");
				$(data.elem).parent().children('.markup_input').attr("lay-verify","required|number");
				$(data.elem).parent().children('.markup_input').focus();
			}else{
				$(data.elem).parent().children(".markup_input").attr("disabled","disabled");
				$(data.elem).parent().children(".markup_input").removeAttr("required");
				$(data.elem).parent().children('.markup_input').attr("lay-verify","");
			}
		});

		// 监听 商品添加/商品修改页，规格分类切换
		element.on('tab(goods_spec_tab)', function(data){
			// 切换tab时
			// 禁用未选中分类规格
			$(data.elem).find('.markup_input').attr("disabled","disabled");
			$(data.elem).find('.markup_input').removeAttr("required");
			$(data.elem).find('.markup_input').attr("lay-verify","");
			// 取消未选中分类规格的选中状态
			var child_checkboxs = $(data.elem).find('input[type="checkbox"]');
			child_checkboxs.each(function(index, item){
				item.checked = false;
			});
			// 启用已选中分类规格
			$(this).find('.markup_input').removeAttr("disabled");
			$(this).find('.markup_input').attr("required","required");
			$(this).find('.markup_input').attr("lay-verify","required");
			// 更新渲染
			form.render('checkbox');
		});
	});
});

var siteurl = 'http://' + window.location.host +'/';

//提示
function mn_dialog_msg(status,msg,time) {
	var time = arguments[2] ? arguments[2] : 3000;
	layer.msg(msg, {icon: status,time});
}

//提示并跳转页面
function mn_redirect_msg(status,msg,url,time) {
	var time = arguments[3] ? arguments[3] : 1500;
	layer.msg(msg,{icon: status,time: time},function(){
		window.location.href = url;
	});
}

//确认对话框
function mn_confirm_url(title, url){
	layer.confirm(title, {
		btn: ['确定','取消'] //按钮
	}, function(){
		window.location.href = url;
	}, function(){
		
	});
}

//确认并ajax提交相关操作
function mn_confirm_set_all(title,action){
	layer.confirm(title, {
		btn: ['确定','取消'] //按钮
	}, function(){
		admin_action(action);
		var _data = $("#myform").serialize();
		var _url = window.location.href;
		if ((_data.split("ids")).length - 1 <= 0) {
			layer.msg('你还没有选择呢', {icon: 2});
			return false;
		}
		$.ajax({
			type: "POST",
			dataType: "json",
			url: _url,
			data: _data,
			success: function(data) {
				if (data.status == 1) {
					mn_redirect_msg(1,data.code, _url);
				}else{
					mn_dialog_msg(2,data.code);
				}
			},
			error: function(HttpRequest, ajaxOptions, thrownError) {}
		});
	}, function(){
		
	});
}

//获取操作类型
function admin_action(action){
	var _action = '';
	if($.trim(action) == 'del'){
		_action = $.trim(action);
	}else if($.trim(action) == 'update'){
		_action = $.trim(action);
	}else if($.trim(action) == 'order'){
		_action = $.trim(action);
	}else if($.trim(action) == 'move'){
		_action = $.trim(action);
	}else if($.trim(action) == 'back'){
		_action = $.trim(action);
	}else{
		_action = '';
	}
	$('#action').val(_action);
}

// 获取汉字对应的拼音
function mn_topinyin(name, from, letter) {
    var val = $("#mn_" + from).val();
    if ($("#mn_" + name).val()) {
        return false;
    }
    $.get(siteurl + "api/pinyin/topinyin?name=" + val + "&rand=" + Math.random(),
    function(data) {
        $("#mn_" + name).val(data);
        if (letter) {
            $("#mn_letter").val(data.substr(0, 1))
        }
    })
}

// 上传文件
function mn_upload_files(field_name, is_image, count) {
	if(field_name == '' || field_name == "undefined"){
		alert('id不能为空');
	}
	var is_image = arguments[1] ? arguments[1] : 0;
	var count = arguments[2] ? arguments[2] : 1;
	layer.open({
		type: 2,
		title: '文件上传',
		maxmin: true,
		shade: 0.7,
		area : ['800px' , '600px'],
		content: siteurl + 'api/upload/webuploader/name/'+field_name+'/is_image/'+is_image+'/count/'+count+'.html'
	});
}

// 删除缩略图
function mn_del_thumb(field_name,default_img){
	if(field_name == '' || field_name == "undefined"){
		alert('id不能为空');
	}
	$('#field_'+field_name+'_url').val('');
	$('#field_'+field_name+'_path').attr('src',default_img);
}
